/*
 * 版权所有 (C) 2015 知启蒙(ZHIQIM) 保留所有权利。[遇见知启蒙，邂逅框架梦，本文采用木兰宽松许可证第2版]
 * 
 * https://zhiqim.org/project/zhiqim_kit/zhiqim_git.htm
 *
 * ZhiqimGit is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */
package org.zhiqim.git.util;

import java.io.File;
import java.io.IOException;

import org.zhiqim.git.Git;
import org.zhiqim.git.GitConstants;
import org.zhiqim.git.GitServer;
import org.zhiqim.git.command.InitCommand;

import org.zhiqim.kernel.Global;
import org.zhiqim.kernel.util.Asserts;
import org.zhiqim.kernel.util.Files;

/**
 * Git服务静态方法类
 *
 * @version v1.0.0 @author zouzhigang 2017-3-27 新建与整理
 */
public class Gits implements GitConstants
{
    /** 获取引用短名称 */
    public static String shortenRefName(String refName)
    {
        if (refName.startsWith(P_REFS_HEADS_))
            return refName.substring(P_REFS_HEADS_.length());
        if (refName.startsWith(P_REFS_TAGS_))
            return refName.substring(P_REFS_TAGS_.length());
        if (refName.startsWith(P_REFS_REMOTES_))
            return refName.substring(P_REFS_REMOTES_.length());
        return refName;
    }
    
    /**
     * 通过Git仓库，如/zhiqim/zhiqim.git获取Git仓库实例
     * 
     * @param gitPath       仓库相对根路径
     * @return              仓库实例
     * @throws IOException  IO异常
     */
    public static Git git(String gitPath)
    {
        return Global.getService(GitServer.class).git(gitPath);
    }
    
    /**
     * 创建Git仓库
     * 
     * @param gitPath   Git相对根路径
     * @return          Git对象
     */
    public static Git create(String gitPath, String name, String email)
    {
        Asserts.asserts(gitPath != null && gitPath.endsWith(".git"), "传入的仓库名称不合法", gitPath);
        
        GitServer server = Global.getService(GitServer.class);
        String gitRootPath = server.getRootPath(gitPath);
        
        try
        {
            Git git = new InitCommand()
                .setDirectory(new File(gitRootPath))
                .setBare(true)
                .setName(name)
                .setEmail(email)
                .call();
            
            server.add(gitRootPath, git);
            return git;
        }
        catch (RuntimeException e)
        {
            throw e;
        }
        catch (Exception e)
        {
            throw new RuntimeException(e);
        }
    }
    
    /**
     * 删除Git仓库
     * 
     * @param gitPath   Git相对根路径
     */
    public static void delete(String gitPath)
    {
        Asserts.asserts(gitPath != null && gitPath.endsWith(".git"), "传入的仓库名称不合法", gitPath);
        
        GitServer server = Global.getService(GitServer.class);
        String gitRootPath = Global.getService(GitServer.class).getRootPath(gitPath);
        
        try
        {
            server.remove(gitRootPath);
            Files.deleteDirectory(gitRootPath);
        }
        catch (Exception e)
        {
            throw new RuntimeException(e);
        }
    }
}
